package com.eastfair.messagecenter.duty;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReUtil;
import com.alibaba.fastjson.JSON;
import com.eastfair.core.base.R;
import com.eastfair.messagecenter.domain.TaskInfo;
import com.eastfair.messagecenter.exceptioncode.MessageServiceExceptionCode;
import com.eastfair.messagecenter.pipeline.BusinessProcess;
import com.eastfair.messagecenter.pipeline.ProcessContext;
import com.eastfair.messagecenter.pipeline.SendTaskModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author ligang
 * <p>
 * 后置参数检查
 *
 * 如 夜间发送改为 早上8点发送，发送推迟队列
 */
@Slf4j
@Service
public class AfterParamCheckAction implements BusinessProcess<SendTaskModel> {


    @Override
    public void process(ProcessContext<SendTaskModel> context) {
        SendTaskModel sendTaskModel = context.getProcessModel();
        List<TaskInfo> taskInfo = sendTaskModel.getTaskInfoList();
        if (CollUtil.isEmpty(taskInfo)) {
            context.setNeedBreak(true).setResponse(R.fail(MessageServiceExceptionCode.CLIENT_BAD_PARAMETERS));
        }
    }


    /**
     * 利用正则过滤掉不合法的接收者
     *
     * @param taskInfo
     * @param regexExp
     */
    private void filter(List<TaskInfo> taskInfo, String regexExp) {
//        Iterator<TaskInfo> iterator = taskInfo.iterator();
//        while (iterator.hasNext()) {
//            TaskInfo task = iterator.next();
//            Set<String> illegalPhone = task.getReceiver().stream()
//                    .filter(phone -> !ReUtil.isMatch(regexExp, phone))
//                    .collect(Collectors.toSet());
//
//            if (CollUtil.isNotEmpty(illegalPhone)) {
//                task.getReceiver().removeAll(illegalPhone);
//                log.error("messageTemplateId:{} find illegal receiver!{}", task.getMessageTemplateId(), JSON.toJSONString(illegalPhone));
//            }
//            if (CollUtil.isEmpty(task.getReceiver())) {
//                iterator.remove();
//            }
//        }
    }

}
